package yyy.ab.modules.security.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import yyy.ab.modules.system.entity.dto.SysRoleSmallDto;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Getter
@Setter
@AllArgsConstructor
public class SecurityUserDto implements UserDetails {

    /**
     * 用户id
     */
    private Long userId;

    /**
     * 用户名
     */
    private String username;
    /**
     * 密码
     */
    private String password;
    /**
     * 权限集合
     */
    private Collection<? extends GrantedAuthority> authorities;

//    /**
//     * 写入权限
//     *
//     * @param roleSmallDtos 资源角色Map
//     */
//    public void writeAuthority(List<SysRoleSmallDto> roleSmallDtos) {
//        if (roleSmallDtos != null) {
//            Collection<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
//            for (SysRoleSmallDto dto : roleSmallDtos) {
//                grantedAuthorities.add(new SimpleGrantedAuthority(dto.getName()));
//            }
//            setAuthorities(grantedAuthorities);
//        }
//    }

    /**
     * 写入权限
     *
     * @param permissions 菜单权限集合
     */
    public void writeAuthority(List<String> permissions) {
        if (permissions != null) {
            Collection<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
            for (String permission : permissions) {
                grantedAuthorities.add(new SimpleGrantedAuthority(permission));
            }
            setAuthorities(grantedAuthorities);
        }
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    public void setAuthorities(Collection<? extends GrantedAuthority> authorities) {
        this.authorities = authorities;
    }
}
